﻿using DevExpress.Data.Utils;
using HalconDotNet;
using ImageDefine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace ImageToolMatch
{
    public partial class FormSetting : DevExpress.XtraEditors.XtraForm
    {
        ImageTask imageTask;
        ImageToolMatch imageTool;
        ImageActionerMatch imageActionerMatch;

        
        public FormSetting()
        {
            InitializeComponent();
        }
        public FormSetting(ImageTask imageTaskIn, ImageToolMatch imageToolIn,ImageActionerMatch imageActionerMatchIn)
        {
            InitializeComponent();
            imageTask = imageTaskIn;
            imageTool = imageToolIn;
            imageActionerMatch = imageActionerMatchIn;
        }

        private void FormSetting_Load(object sender, EventArgs e)
        {
            propertyGridControl1.SelectedObject = imageTool.Setting;

            imageTask.LoadRegeditFile(smartDisplayControl1);

            if (imageTool.Setting.hXLDCont == null)
            {
                imageTool.Setting.hXLDCont = new HXLDCont();
                imageTool.Setting.hXLDCont.GenEmptyObj();
            }
                
        }

        private void simpleButton画模板区域_Click(object sender, EventArgs e)
        {
            if (smartDisplayControl1.ho_Image == null)
                return;
            if (imageTool.Setting.regionModel == null)
            {
                smartDisplayControl1.CreateRectangle1();
            }
            else
            {
                int iRow1, iCol1, iRow2, iCol2;
                if (imageTool.Setting.regionModel.CountObj() > 0)
                {
                    imageTool.Setting.regionModel.SmallestRectangle1(out iRow1, out iCol1, out iRow2, out iCol2);
                    smartDisplayControl1.CreateRectangle1(iRow1, iCol1, iRow2, iCol2);
                }
                else
                {
                    smartDisplayControl1.CreateRectangle1();
                }
                
            }
            smartDisplayControl1.drawRectangle1Finished += SmartDisplayControl1_drawRectangle1Finished;
        }

        private void SmartDisplayControl1_drawRectangle1Finished(double dRow1, double dCol1, double dRow2, double dCol2)
        {
            smartDisplayControl1.drawRectangle1Finished -= SmartDisplayControl1_drawRectangle1Finished;
            imageTool.Setting.regionModel = new HalconDotNet.HRegion(dRow1, dCol1, dRow2, dCol2);
            smartDisplayControl1.DisplayRectangle1(dRow1, dCol1, dRow2, dCol2, "green");
        }

        private void simpleButton画屏蔽区域_Click(object sender, EventArgs e)
        {
            if (smartDisplayControl1.ho_Image == null)
                return;
            if (imageTool.Setting.regionIgnore == null)
            {
                smartDisplayControl1.CreateRectangle1();
            }
            else
            {
                int iRow1, iCol1, iRow2, iCol2;
                if (imageTool.Setting.regionIgnore.CountObj() > 0)
                {
                    imageTool.Setting.regionIgnore.SmallestRectangle1(out iRow1, out iCol1, out iRow2, out iCol2);
                    smartDisplayControl1.CreateRectangle1(iRow1, iCol1, iRow2, iCol2);
                }
                else
                {
                    smartDisplayControl1.CreateRectangle1();
                }
            }
            smartDisplayControl1.drawRectangle1Finished += SmartDisplayControl1_drawRectangle1Finished1;

        }

        private void SmartDisplayControl1_drawRectangle1Finished1(double dRow1, double dCol1, double dRow2, double dCol2)
        {
            smartDisplayControl1.drawRectangle1Finished -= SmartDisplayControl1_drawRectangle1Finished1;
            imageTool.Setting.regionIgnore = new HalconDotNet.HRegion(dRow1, dCol1, dRow2, dCol2);
            smartDisplayControl1.DisplayRectangle1(dRow1, dCol1, dRow2, dCol2, "red");
        }

        private void simpleButton画搜索区域_Click(object sender, EventArgs e)
        {
            if (smartDisplayControl1.ho_Image == null)
                return;
            if (imageTool.Setting.SRow1 ==0)
            {
                smartDisplayControl1.CreateRectangle1();
            }
            else
            {
                smartDisplayControl1.CreateRectangle1(imageTool.Setting.SRow1, imageTool.Setting.SCol1, imageTool.Setting.SRow2, imageTool.Setting.SCol2);
            }
            smartDisplayControl1.drawRectangle1Finished += SmartDisplayControl1_drawRectangle1Finished2; ;
        }

        private void SmartDisplayControl1_drawRectangle1Finished2(double dRow1, double dCol1, double dRow2, double dCol2)
        {
            smartDisplayControl1.drawRectangle1Finished -= SmartDisplayControl1_drawRectangle1Finished2;
            imageTool.Setting.SRow1 = (int)dRow1;
            imageTool.Setting.SCol1 = (int)dCol1;
            imageTool.Setting.SRow2 = (int)dRow2;
            imageTool.Setting.SCol2 = (int)dCol2;
            smartDisplayControl1.DisplayRectangle1(dRow1, dCol1, dRow2, dCol2, "blue");
        }

        private void simpleButton创建模板_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);
            imageTool.actionerMatch.CreateShapeModel(smartDisplayControl1.ho_Image, smartDisplayControl1);
            propertyGridControl1.Refresh();
        }

        private void simpleButtonLoadImage_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);
            imageTool.Run(smartDisplayControl1.ho_Image, smartDisplayControl1);
        }

        private void simpleButton开始画图_Click(object sender, EventArgs e)
        {
            if(imageTool.Setting.hXLDCont==null)
                imageTool.Setting.hXLDCont=new HXLDCont();
            imageTool.Setting.hXLDCont.GenEmptyObj();
        }

        private void simpleButton显示轮廓_Click(object sender, EventArgs e)
        {

            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);
            smartDisplayControl1.DispObj(imageTool.Setting.hXLDCont, "green");

        }

        private void simpleButton添加轮廓_Click(object sender, EventArgs e)
        {
            int iCount = imageTool.Setting.hXLDCont.CountObj();
            
            if (iCount == 0)
            {
                smartDisplayControl1.CreateXLD();
            }
            else
            {
                HXLDCont endXld = imageTool.Setting.hXLDCont.SelectObj(iCount);
                HalconDotNet.HTuple tupleRow;
                HalconDotNet.HTuple tupleCol; ;
                endXld.GetContourXld(out tupleRow, out tupleCol);

                smartDisplayControl1.CreateXLD(new double[] { tupleRow.DArr[tupleRow.Length - 1], tupleRow.DArr[tupleRow.Length - 1] +50}, new double[] { tupleCol.DArr[tupleCol.Length - 1] , tupleCol.DArr[tupleCol.Length - 1]+50});
            }
            
            smartDisplayControl1.drawXldFinished += SmartDisplayControl1_drawXldFinished;
        }

        private void SmartDisplayControl1_drawXldFinished(double[] dRows, double[] dCols)
        {
            smartDisplayControl1.drawXldFinished -= SmartDisplayControl1_drawXldFinished;
            HalconDotNet.HTuple tupleRow = new HTuple(dRows);
            HalconDotNet.HTuple tupleCol = new HTuple(dCols);
            HXLDCont newHXld = new HXLDCont(tupleRow, tupleCol);
            imageTool.Setting.hXLDCont = imageTool.Setting.hXLDCont.ConcatObj(newHXld);

            smartDisplayControl1.DispObj(imageTool.Setting.hXLDCont, "green");

        }
        private void simpleButton创建模板XLD_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);

            if (imageTool.actionerMatch.CreateShapeModel(smartDisplayControl1.ho_Image, smartDisplayControl1, imageTool.Setting.hXLDCont))
            {
                smartDisplayControl1.DispObj(imageTool.Setting.hXLDCont, "green");
            }
            propertyGridControl1.Refresh();
        }

        private void simpleButton查看金子塔轮廓_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);
           

            HObject ho_ShapeModelImages;
            HObject ho_ShapeModelRegions;
            HOperatorSet.GenEmptyObj(out ho_ShapeModelImages);
            HOperatorSet.GenEmptyObj(out ho_ShapeModelRegions);

           
            int iWidth, iHeight;
            string strType = "";
            smartDisplayControl1.ho_Image.GetImagePointer1(out strType, out iWidth, out iHeight);
            HalconDotNet.HImage imageProcess = new HImage(strType, iWidth, iHeight);



            imageProcess = smartDisplayControl1.ho_Image.CopyImage();

            HOperatorSet.InspectShapeModel(imageProcess, out ho_ShapeModelImages, out ho_ShapeModelRegions, imageTool.Setting.NumLevels, imageTool.Setting.MinContrast);
            smartDisplayControl1.DispObj(ho_ShapeModelRegions[imageTool.Setting.金字塔最低层], "green");
        }

        private void simpleButton设置偏移位置_Click(object sender, EventArgs e)
        {
            if (smartDisplayControl1.ho_Image == null)
                return;
           smartDisplayControl1.CreateCircle((int)(imageTool.Setting.RefRow + imageTool.Setting.MarkerYOffSet), (int)(imageTool.Setting.RefCol+ imageTool.Setting.MarkerXOffSet), 30);
           smartDisplayControl1.drawCircleFinished += SmartDisplayControl1_drawCircleFinished;


        }

        private void SmartDisplayControl1_drawCircleFinished(double Row, double Col, double R)
        {
            imageTool.Setting.MarkerXOffSet = Col - imageTool.Setting.RefCol;
            imageTool.Setting.MarkerYOffSet = Row - imageTool.Setting.RefRow;
            smartDisplayControl1.drawCircleFinished -= SmartDisplayControl1_drawCircleFinished;
            propertyGridControl1.Refresh();
            HalconDotNet.HXLDCont crossXLD = new HalconDotNet.HXLDCont();
            crossXLD.GenCrossContourXld(Row, Col, 30, 0.0);
            smartDisplayControl1.DispObj(crossXLD);
        }
    }
}
